package com.gccloud.design;

import com.gccloud.design.entity.SdRepoActionConfEntity;
import com.gccloud.design.service.SdRepoActionConfService;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.vo.PageVO;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 环节操作配置
 *
 * @author li.tao2
 */
@RestController
@RequestMapping("/sdRepoActionConf")
@Api(tags = "环节操作配置API")
@ApiSort(value = 12)
public class SdRepoActionConfController {

    @Resource
    private SdRepoActionConfService sdRepoActionConfService;

    private static final Logger log = LoggerFactory.getLogger(SdRepoActionConfController.class);

    /**
     * 环节操作配置：获取环节操作按钮列表
     */
    @GetMapping("/getLinkButton")
    @ApiOperation(value = "获取环节操作按钮列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "long"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "long"),
            @ApiImplicitParam(name = "tacheId", value = "环节ID", paramType = "query", dataType = "string")
    })
    public R<Map<String, Object>> getLinkButton(String tacheId, PageVO pageObj) {
        Map<String, Object> dataMap = sdRepoActionConfService.getLinkButton(tacheId, pageObj);
        return R.success(dataMap);
    }

    /**
     * 根据环节id获取按钮列表
     */
    @GetMapping("/getButtonByRepoId")
    @ApiOperation(value = "根据环节id获取按钮列表", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Map<String, Object>> getButtonByRepoId(String repoId) {
        Map<String, Object> dataMap = sdRepoActionConfService.getButton(repoId);
        return R.success(dataMap);
    }

    /**
     * 环节操作配置：查询环节未配置的操作按钮
     */
    @GetMapping("/queryButtonByTacheId")
    @ApiOperation(value = "查询环节未配置的操作按钮", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "long"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "long"),
            @ApiImplicitParam(name = "tacheId", value = "环节ID", paramType = "query", dataType = "string")
    })
    public R<Map<String, Object>> queryButtonByTacheId(String tacheId, PageVO pageObj) {
        Map<String, Object> dataMap = sdRepoActionConfService.queryButtonByTacheId(tacheId, pageObj);
        return R.success(dataMap);
    }

    /**
     * 环节操作配置：为某个环节添加按钮
     */
    @ApiOperation(value = "为某个环节添加按钮")
    @PostMapping(value = "/addButtonToLink", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tacheId", value = "环节id", paramType = "query", required = true, dataType = "string"),
            @ApiImplicitParam(name = "ids", value = "按钮id", paramType = "query", required = true, dataType = "string")
    })
    public R<Boolean> addButtonToLink(@RequestParam(value = "tacheId") String tacheId, @RequestParam(value = "ids") String ids, @RequestParam(value = "processDefine") String processDefine) {
        String[] arr = ids.split(",");
        List<SdRepoActionConfEntity> sdRepoActionConfEntityList = new LinkedList<>();
        //获取当前环节最后一个显示按钮的顺序
        Integer displayOrder = sdRepoActionConfService.getMaxShowLinkButton(tacheId);
        if (displayOrder == null) {
            displayOrder = 0;
        }
        //数据封装
        for (int i = 0; i < arr.length; i++) {
            SdRepoActionConfEntity sdRepoActionConfEntity = new SdRepoActionConfEntity();
            sdRepoActionConfEntity.setAreaId(34);
            sdRepoActionConfEntity.setProcessDefId(processDefine);
            sdRepoActionConfEntity.setActionId(Integer.parseInt(arr[i]));
            sdRepoActionConfEntity.setActivityRepoId(tacheId);
            sdRepoActionConfEntity.setDisplayOrder(displayOrder + i + 1);
            sdRepoActionConfEntityList.add(sdRepoActionConfEntity);
        }
        try {
            boolean result = sdRepoActionConfService.addButtonToLink(sdRepoActionConfEntityList);
            return R.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("增加环节按钮失败," + e.getMessage());
            return R.success(false);
        }
    }

    /**
     * 环节操作配置：按钮上移
     */
    @ApiOperation(value = "按钮上移")
    @PostMapping(value = "/upMove", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tacheId", value = "环节id", paramType = "query", required = true, dataType = "string"),
            @ApiImplicitParam(name = "ids", value = "按钮id", paramType = "query", required = true, dataType = "string")
    })
    public R<Boolean> upMove(@RequestParam(value = "tacheId") String tacheId, @RequestParam(value = "ids") String ids) {
        boolean result = true;
        String[] arr = ids.split(",");
        for (String id : arr) {
            boolean res = sdRepoActionConfService.upMove(tacheId, Integer.valueOf(id));
            result = result && res;
        }
        return R.success(result);
    }

    /**
     * 环节操作配置：按钮下移
     */
    @ApiOperation(value = "按钮下移")
    @PostMapping(value = "/downMove", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tacheId", value = "环节id", paramType = "query", required = true, dataType = "string"),
            @ApiImplicitParam(name = "ids", value = "按钮id", paramType = "query", required = true, dataType = "string")
    })
    public R<Boolean> downMove(@RequestParam(value = "tacheId") String tacheId, @RequestParam(value = "ids") String ids) {
        boolean result = true;
        String[] arr = ids.split(",");
        for (String id : arr) {
            boolean res = sdRepoActionConfService.downMove(tacheId, Integer.valueOf(id));
            result = result && res;
        }
        return R.success(result);
    }

    /**
     * 环节操作配置:删除环节按钮
     */
    @ApiOperation(value = "删除环节按钮")
    @PostMapping(value = "/deleteButtonToLink", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tacheId", value = "环节id", paramType = "query", required = true, dataType = "string"),
            @ApiImplicitParam(name = "ids", value = "按钮id", paramType = "query", required = true, dataType = "string")
    })
    public R<Boolean> deleteButtonToLink(@RequestParam(value = "tacheId") String tacheId, @RequestParam(value = "ids") String ids) {
        boolean result = sdRepoActionConfService.deleteButtonToLink(tacheId, ids);
        return R.success(result);
    }

}
